package cn.wangxl.vertx.starter.web.handleerror;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpServer;
import io.vertx.ext.web.Route;
import io.vertx.ext.web.Router;

/**
 * 路由错误处理
 *
 */
public class HanderError extends AbstractVerticle {

  @Override
  public void start(Future<Void> startFuture) throws Exception {
    Vertx vertx = Vertx.vertx();
    HttpServer server = vertx.createHttpServer();

    Router router = Router.router(vertx);

    router.get("/error1").handler(ctx->{

      throw new RuntimeException();
    });

    router.get("/error2").handler(ctx->{

      throw new RuntimeException();

    }).failureHandler(ctx->{
      String 热水 = "自定义处理：/error2-------出错了";
      System.out.println(热水);
      ctx.response().setStatusCode(500);
      ctx.response().end(热水);
    });


    server.requestHandler(router).listen(9999, http -> {
      if (http.succeeded()) {
        startFuture.complete();
        System.out.println("HTTP server started on port 8888");
      } else {
        startFuture.fail(http.cause());
      }
    });
  }


  public static void main(String[] args) throws Exception {
    new HanderError().start(Future.future());
  }
}
